{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 67,
   "metadata": {},
   "outputs": [],
   "source": [
    "from ortools.linear_solver import pywraplp\n",
    "from ortools.sat.python import cp_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (<ipython-input-70-d23d013b64a5>, line 45)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  File \u001b[1;32m\"<ipython-input-70-d23d013b64a5>\"\u001b[1;36m, line \u001b[1;32m45\u001b[0m\n\u001b[1;33m    for j in range(0,7)\u001b[0m\n\u001b[1;37m                       ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    "def main():\n",
    "    model = cp_model.CpModel()\n",
    "    #基础工资\n",
    "    s=4350\n",
    "    #人数\n",
    "    a=10\n",
    "    #惩罚\n",
    "    C1=15\n",
    "    C2=10\n",
    "    var_upper_bound=100000\n",
    "    #目标\n",
    "    #infinity = solver.infinity()\n",
    "    Z= model.NewIntVar(0, var_upper_bound, 'Z')\n",
    "    #定义每日完成的订单量\n",
    "    P=[0 for j in range(0,7)]\n",
    "    #定义二维员工排班表\n",
    "    x=[[0 for j in range(0,14)] for i in range(0,a)]\n",
    "    #定义每白（夜）有多少人上班\n",
    "    day=[0 for j in range(0,14)]\n",
    "    #定义每人一周一共出勤次数减去六次\n",
    "    sum=[0 for j in range(0,7)]\n",
    "    #定义每日所需做的订单量数组\n",
    "    Q=[300,350,320,370,280,300,320]\n",
    "    #对于每个人每天出勤情况\n",
    "    for i in range(0,a):\n",
    "        for j in range(0,7):\n",
    "            #定义变量初始化\n",
    "            x[i][2*j]= model.NewBoolVar('x_'+str(i)+'_'+str(2j))\n",
    "            x[i][2*j+1] = model.NewBoolVar('x_'+str(i)+'_'+str(2j+1))\n",
    "    for j in range(0,14):\n",
    "        for i in range(0,a):\n",
    "            day[j]+=x[i][j]\n",
    "        p[j]=day[j]*40\n",
    "    for j in range(0,7):\n",
    "        P[j]=model.NewIntVar(0,var_upper_bound,'p_'+str(j))\n",
    "        day[2*j]=model.NewIntVar(0,10,'day_'+str(2*j))\n",
    "        day[2*j+1]=model.NewIntVar(0,10,'day_'+str(2*j+1))\n",
    "    #约束\n",
    "    #x[i][2j]+x[i][2j+1]<=1\n",
    "    for i in range(0,a):\n",
    "        for j in range(0,7):\n",
    "            model.add(x[i][2*j]+x[i][2*j+1]<=1)\n",
    "            sum[i]+=x[i][2*j]+x[i][2*j+1]\n",
    "        sum[i]=sum[i]-6\n",
    "    for j in range(0,7)\n",
    "        model.add(day[2*j]>=1.5*day[2*j+1])\n",
    "    for j in range(0,7):\n",
    "        k1=max((Q[j]-P[j]),0)\n",
    "        k2=max((P[j]-Q[j]),0)\n",
    "    model.Minimize(s*a+2*s/21.75*max(sum,0)+C1*k1+C2*k2)\n",
    "    for j in range(0,7):\n",
    "        model.Minimize(P[j])\n",
    "    solver=cp_model.CpSolver()\n",
    "    status=solver.Solve(model)\n",
    "    if status==cp_model.OPTIMAL:\n",
    "        print('Minimum of objective function:%i'%solver.ObjectiveValue())\n",
    "        print()\n",
    "        for i in range(0,a):\n",
    "            for j in range(0,14):\n",
    "                print(x[i][j])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 69,
   "metadata": {},
   "outputs": [
    {
     "ename": "IndexError",
     "evalue": "list index out of range",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mIndexError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-69-4be4dd58a851>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0m__name__\u001b[0m\u001b[1;33m==\u001b[0m\u001b[1;34m'__main__'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 2\u001b[1;33m     \u001b[0mmain\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;32m<ipython-input-56-5b1d3b90cb4c>\u001b[0m in \u001b[0;36mmain\u001b[1;34m()\u001b[0m\n\u001b[0;32m     30\u001b[0m     \u001b[1;32mfor\u001b[0m \u001b[0mi\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[0ma\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     31\u001b[0m         \u001b[1;32mfor\u001b[0m \u001b[0mj\u001b[0m \u001b[1;32min\u001b[0m \u001b[0mrange\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m0\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m14\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m---> 32\u001b[1;33m             \u001b[0mp\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m+=\u001b[0m\u001b[1;36m40\u001b[0m\u001b[1;33m*\u001b[0m\u001b[0mx\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mi\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m[\u001b[0m\u001b[0mj\u001b[0m\u001b[1;33m]\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m     33\u001b[0m     \u001b[1;31m#约束\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m     34\u001b[0m     \u001b[1;31m#x[i][2j]+x[i][2j+1]<=1\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n",
      "\u001b[1;31mIndexError\u001b[0m: list index out of range"
     ]
    }
   ],
   "source": [
    "if __name__=='__main__':\n",
    "    main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
